package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.nls.service.facade.AutoOutbdTaskService;
import com.fdb.efp.nls.service.vo.AutoOutbdTaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class AutoOutbdTaskController extends AbstractController {

    @Autowired
    @Qualifier("autoOutbdTaskService")
    private AutoOutbdTaskService autoOutbdTaskService;
    @Autowired
    @Qualifier("sRoleDataRuleService")
    private SRoleDataRuleService sRoleDataRuleService;

    /**
     * 列表数据查询
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/auto/outbd/tasks",method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<AutoOutbdTaskVO>> queryAutoOutbdTaskAll(@RequestBody AutoOutbdTaskVO vo) {
        List<AutoOutbdTaskVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        // 根据权限方案调用不同的方案进行数据查询
        switch (dataRuleType) {
            case Owner:
                outputVo = autoOutbdTaskService.queryAllOwner(vo);
                break;
            case CurrOrg:
                outputVo = autoOutbdTaskService.queryAllCurrOrg(vo);
                break;
            case CurrDownOrg:
                outputVo = autoOutbdTaskService.queryAllCurrDownOrg(vo);
                break;
            case CurrOwnerPrd:
                outputVo = autoOutbdTaskService.queryAllCurrOwnerPrd(vo);
                break;
            default:
                outputVo = autoOutbdTaskService.queryAllOwner(vo);
                break;
        }
        return getResponseData(outputVo);

    }

    /**
     * 根据主键查询详情
     * @return
     */
    @RequestMapping(value="/auto/outbd/task", method=RequestMethod.GET)
    public @ResponseBody
    ResponseData<AutoOutbdTaskVO> queryByPk(@PathVariable("colltTaskNo") String colltTaskNo) {
        AutoOutbdTaskVO vo = new AutoOutbdTaskVO();
        vo.setColltTaskNo(colltTaskNo);
        AutoOutbdTaskVO outputVo = autoOutbdTaskService.queryByPk(vo);
        return getResponseData(outputVo);
    }

    /**
     * 根据主键删除信息
     * @param autoOutbdTask
     * @return
     */
    @RequestMapping(value="/auto/outbd/task", method=RequestMethod.DELETE)
    public @ResponseBody
    ResponseData<Integer> deleteByPk(@RequestBody AutoOutbdTaskVO inAutoOutbdTaskVo) {
        int outputVo = autoOutbdTaskService.deleteByPk(inAutoOutbdTaskVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键更新信息
     * @param inAutoOutbdTaskVo
     * @return
     */
    @RequestMapping(value="/auto/outbd/task", method=RequestMethod.PUT)
    public @ResponseBody
    ResponseData<Integer> updateByPk(@RequestBody AutoOutbdTaskVO inAutoOutbdTaskVo) {
        int outputVo = autoOutbdTaskService.updateByPk(inAutoOutbdTaskVo);
        return getResponseData(outputVo);

    }

    /**
     * 新增数据
     * @param inAutoOutbdTaskVo
     * @return
     */
    @RequestMapping(value="/auto/outbd/task", method=RequestMethod.POST)
    public @ResponseBody
    ResponseData<Integer> insertAutoOutbdTask(@RequestBody AutoOutbdTaskVO inAutoOutbdTaskVo) {
        int outputVo = autoOutbdTaskService.insertAutoOutbdTask(inAutoOutbdTaskVo);
        return getResponseData(outputVo);

    }
}
